package com.lcs.springcloud.controller;

import com.lcs.springcloud.entity.OrderTbl;
import com.lcs.springcloud.feign.StockOpenFeign;
import com.lcs.springcloud.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    StockOpenFeign stockOpenFeign;

    @RequestMapping("/add")
    @GlobalTransactional
    public String add(){

        OrderTbl orderTbl = new OrderTbl();
        orderTbl.setProduct_id("10");
        orderTbl.setTotal_amount(new BigDecimal(3000));
        orderTbl.setStatu(0);

        //1. 插入订单
        orderService.insert(orderTbl);

        //2. 扣减库存
        String reduct = stockOpenFeign.reduct(orderTbl.getProduct_id());

        // 3. 出现问题
         int a= 1/0;
        return "add order "+reduct;
    }

    /**
     * 获取id获取数据
     * @param id
     * @return
     */
    @RequestMapping("/get")
    public OrderTbl get(Integer id) throws InterruptedException {
        //延迟2秒
        TimeUnit.SECONDS.sleep(2);
        return  orderService.get(id);
    }
}
